package com.gjb6.customer.utils.permssion;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.UiThread;
import android.support.v4.app.ActivityCompat;

/**
 * 请在权限请求回调方法内调用此方法
 * Created by Andy Hong on 9/26/2017.
 */
@UiThread
public class PermissionResult {
    private String[] mPermissions;
    private int[] grantResults;
    private Activity activity;

    public PermissionResult(Activity activity, String[] permissions, int[] grantResults) {
        this.mPermissions = permissions;
        this.grantResults = grantResults;
        this.activity = activity;
    }

    /**
     * 是否通过
     */
    public boolean isGranted() {
        if (mPermissions == null || mPermissions.length == 0) {
            return true;
        }
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否被永久拒绝
     */
    public boolean isDeniedForever() {
        if (mPermissions == null || mPermissions.length == 0) {
            return false;
        }
        for (int i = 0; i < grantResults.length; i++) {
            if (isDeniedForever(mPermissions[i], grantResults[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 指定权限是否接受
     *
     * @param permissions 权限列表
     */
    public boolean isGranted(String... permissions) {
        if (permissions == null || permissions.length == 0) {
            return true;
        }
        for (int i = 0; i < permissions.length; i++) {
            int index = findPermissionIndexInResult(permissions[i]);
            if (index == -1) {
                if (ActivityCompat.checkSelfPermission(activity, permissions[i]) == PackageManager.PERMISSION_DENIED) {
                    return false;
                }
            } else {
                if (grantResults[index] == PackageManager.PERMISSION_DENIED) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 指定权限是否被永久拒绝
     *
     * @param permissions 权限列表
     */
    public boolean isDeniedForever(String... permissions) {
        if (permissions == null || permissions.length == 0) {
            return true;
        }
        for (int i = 0; i < permissions.length; i++) {
            int index = findPermissionIndexInResult(permissions[i]);
            if (index == -1) {
                if (isDeniedForever(permissions[i], ActivityCompat.checkSelfPermission(activity, permissions[i]))) {
                    return true;
                }
            } else {
                if (isDeniedForever(mPermissions[index], grantResults[index])) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean isDeniedForever(String permission, int grantResult) {
        return grantResult == PackageManager.PERMISSION_DENIED && !ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
    }

    private int findPermissionIndexInResult(String permission) {
        for (int i = 0; i < mPermissions.length; i++) {
            if (mPermissions[i].equals(permission)) {
                return i;
            }
        }
        return -1;
    }
}
